<?php
/**
 * 后台轮播图管理
 * lixinguo
 * 2017-7-10
 */
namespace Admin\Controller;
use Common\Controller\AdminbaseController;
class OrderController extends AdminbaseController
{
	//订单列表
	public function index(){
		$where = array(
			'o.status' => array('gt',0)
		);

        if($start = $_POST['start_time']){
			$where['o.create_time'] = array('gt',strtotime($start));
			$this->assign('start_time',$start);
		}
		if($end = $_POST['end_time']){
			$where['o.create_time'] = array('lt',strtotime($end));
			$this->assign('end_time',$start);
		}
        if($_POST['start_time'] && $_POST['end_time']){
			$start = $_POST['start_time'];
			$end = $_POST['end_time'];
			$this->assign('start_time',$start);
			$this->assign('end_time',$end);
			$where['o.create_time'] = array('between',strtotime($start).','.strtotime($end));
		}
        if($sn = $_POST['order_num']){
		   $where['o.order_num'] = array('like',"%{$sn}%");
		}
		$count = M('order')->alias('o')
				->where($where)
				->count();
		$page = $this->page($count,$this->listRow);
		$info = M('order')
				->alias('o')
				->join("__MEMBER__ m on o.user_id = m.id")
		        ->where($where)
				->limit($page->firstRow.','.$page->listRows)
				->field("o.*,m.user_name")
				->select();
		$this->assign('page',$page->show());
		$this->assign('info',$info);
		$this->display();
	}


	// 改状态
	public function reState(){
		$id = I("get.id",0,'intval');
		$state = I("get.status",1,'intval');
		if(!D("Order")->where(array('id'=>$id))->save(array('status'=>$state))) $this->error("操作失败");
	    $this->success('操作成功',U('Order/index'));
	    exit();
	}

	//订单详情
	public function detail(){
		$id = I("get.id",0,'intval');
		 $info = M('order')->alias('o')
				 ->join('LEFT JOIN cai_member m on m.id = o.user_id')
				 ->join('LEFT JOIN cai_user_address a on a.id = o.address_id')
		 		->where(array('o.id'=>$id))
		 		->field("o.*,m.user_name,a.name,a.phone,a.address,a.sex,a.area")
				 ->find();
		 $detail = D("OrderDetail")->alias('d')
				 ->join("cai_product as p on d.product_id = p.id")
				 ->where(array('d.order_id'=>$id))
				 ->field("d.product_num,d.product_price,d.product_attr,p.title,p.brand_id as bid")
				 ->select();
        foreach($detail as $k => $v){
		  $detail[$k]['brand'] =	D("Brand")->where(array('id'=>$v['bid']))->getField('title');
		}
        $this->assign('list',$detail);
		$this->assign('order',$info);
 		$this->display();
	}
	//退货
    public function refund(){
	    $order_num = trim($_POST['order_num']);
	    if($order_num){
	        $where['o.order_num'] = array('like','%'.$order_num.'%');
        }
	    $where['o.status'] = array('egt',6);
	    $count = D('order')->alias('o')
            ->join('cai_member m on o.user_id = m.id')
            ->where($where)
            ->count();
	    $page = $this->Page($count,$this->listRow);
	    $info = D('order')->alias('o')
            ->join('cai_member m on o.user_id = m.id')
            ->where($where)
            ->field('o.*,m.user_name,m.phone')
			->limit($page->firstRow.','.$page->listRows)
            ->select();
		$this->assign('info',$info);
		$this->assign('page',$page->show());
	    $this->display();
    }

    public function detail_return(){
        $oid = I("get.oid",0,'intval');
        $info = M("order")->alias('o')
            ->join('cai_member m on o.user_id = m.id')
            ->where(array('o.id'=>$oid))
            ->field('o.*,m.user_name,m.phone')
            ->find();
        $this->assign('info',$info);
        $this->display();
    }
    public function status_refund(){
        $id = $_POST['oid'];
        $status = $_POST['status'];
        $info = M("order")->where(array('id'=>$id))->save(array('status'=>$status));
        if($info!==false){
            $this->success('保存成功',U('order/return'));
        }else{
            $this->error('网络错误');
        }
    }

}
